package com.cxh.rmsq.ui.activity.self

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivityInviteListBinding
import com.cxh.rmsq.databinding.ActivityLookMeBinding
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.net.newNet.TreasureViewModel
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.ui.adapter.self.InviteListAdapter
import com.cxh.rmsq.ui.adapter.self.LookMeListAdapter
import com.cxh.rmsq.utils.ToastUtils
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 邀请列表
 */
class InviteListActivity : TitleBaseActivity() {
    private lateinit var binding: ActivityInviteListBinding

    private var listAdapter: InviteListAdapter? = null
    private var page = 1
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(TreasureViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityInviteListBinding.inflate(layoutInflater)
        setContentView(binding.root)
        titleBar.tvTitle.text = "邀请列表"
        initView()
    }

    fun initView() {
        binding.rvList.setLayoutManager(LinearLayoutManager(this))
        listAdapter = InviteListAdapter(1)
        listAdapter!!.setEmptyView(View.inflate(this, R.layout.empty_data_layout, null))

        binding.rvList.setAdapter(listAdapter)


        binding.refreshList.resetNoMoreData()
        binding.refreshList.setRefreshHeader(MaterialHeader(this))
        binding.refreshList.setRefreshFooter(ClassicsFooter(this))
        binding.refreshList.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                getData()
                refreshLayout.finishLoadMore()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                listAdapter!!.setNewInstance(null)
                getData()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        page = 1
        listAdapter!!.setNewInstance(null)
        getData()
    }

    private fun getData() {
        viewModel.currentUserInviteCodePage(page, 10) {
            if (binding.refreshList.isRefreshing()) {
                binding.refreshList.finishRefresh()
            }
            if (it.data != null) {
                if (page == 1) {
                    listAdapter!!.setNewInstance(it.data.list)
                    if (it.data.list!!.size < 10) {
                        binding.refreshList.setEnableLoadMore(false)
                    } else {
                        binding.refreshList.setEnableLoadMore(true)
                    }
                } else {
                    if (it.data.list!!.size > 0) {
                        listAdapter!!.addData(it.data.list!!)
                        binding.refreshList.setEnableLoadMore(true)
                    } else {
                        binding.refreshList.setEnableLoadMore(false)
                        binding.refreshList.finishLoadMoreWithNoMoreData()
                    }
                }
                listAdapter!!.notifyDataSetChanged()
            }
        }
    }
}